package dkm;

import static org.junit.Assert.assertTrue;
import dkm.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.BattleStartedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * This JUnit Test ensures that PersonalWall is moving along the walls of the battlefield.
 * It does this by checking to make sure that PersonalWalls is moving close to the walls.
 * @author David Mau
 */
public class TestWallHuggingStrategy extends RobotTestBed {

  private int height;
  private int width;
  private boolean nearNorthWall = false;
  private boolean nearWestWall = false;
  private boolean nearEastWall = false;
  private boolean nearSouthWall = false;
  
  /**
   * Specifies that SittingDuck and PersonalWall are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,dkm.PersonalWall";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * Get the rules for the battle when it begins.
   * @param event information stored about the battle when it starts
   */
  public void onBattleStarted(BattleStartedEvent event) {
    // Get the battlefield height and width.
    height = event.getBattleRules().getBattlefieldHeight();
    width = event.getBattleRules().getBattlefieldWidth();
  }

  /**
   * Check where the robot is at the end of each turn.
   * @param event an information snapshot of what is happening when the turn ends
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double horizontal = robot.getX();
    double vertical = robot.getY();
    
    // If PersonalWalls is going along a wall, record that it is.
    if ( horizontal < 40.0 ) {
      nearWestWall = true;
    }
    if ( horizontal < (width - 40.0) ) {
      nearEastWall = true;
    }
    if ( vertical < 40.0 ) {
      nearSouthWall = true;
    }
    if ( vertical < (height - 40.0) ) {
      nearNorthWall = true;
    }

  }

  /**
   * The actual test, which asserts that PersonalWalls has run along the walls.
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue( "Robot has moved along North Wall.", nearNorthWall );
    assertTrue( "Robot has moved along West Wall.", nearWestWall );
    assertTrue( "Robot has moved along East Wall.", nearEastWall );
    assertTrue( "Robot has moved along South Wall.", nearSouthWall );
  }
}